package com.dynamic.manager.controller.admin.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dynamic.model.model.po.SysmanResource;
import com.dynamic.model.service.SystemResourceService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dynamic.model.common.action.BaseController;
import com.dynamic.model.common.service.BaseService;
import com.dynamic.model.common.vo.Page;
import com.dynamic.model.common.vo.Result;


/**
 * 
 * @desc 用户后台资源资源权限表--RBAC权限管理action控制类
 *
 * @author alexgaoyh
 */
@Controller
@RequestMapping(value="admin/systemresource")
public class SystemResourceAction extends BaseController<SysmanResource> {

	private static final Logger LOGGER = Logger.getLogger(SysmanResource.class);
	
	@Override
	@Resource(name = "systemResourceServiceImpl")
	public void setBaseService(BaseService<SysmanResource> baseService) {
		this.baseService = baseService;
	}
	
	private SystemResourceService getService(){
		return (SystemResourceService) this.baseService;
	}
	
	@RequestMapping(value = "toresourcelist")
	@ResponseBody
	public ModelAndView touserlist(ModelAndView model) {
		ModelAndView mav = new ModelAndView();
		Map<String,String> nav=new LinkedHashMap<String,String>();
		nav.put("后台","#");
		nav.put("系统管理","#");
		nav.put("菜单列表","/admin/systemresource/toresourcelist");
		mav.addObject("navmap", nav);
		mav.setViewName("jsp/systemresource");
		return mav;
	}
	
	@RequestMapping(value = "resourcelist")
	@ResponseBody
	public void rolelist(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		String order = request.getParameter("order");
		String name = request.getParameter("departmentname1");
		String type = request.getParameter("departmentname2");
		if(null!=offset&&null!=limit){
			Page<SysmanResource> page=new Page<SysmanResource>();
			page.setOffset(Integer.parseInt(offset));
			page.setLimit(Integer.parseInt(limit));
			page.setOrder(order);
			List<SysmanResource> userlist=this.getService().getPageDataByName(page, name,type);
			page.setRows(userlist);
			this.renderJson(page, response);
		}else{
			Result result=new Result();
			result=Result.fail("参数错误");
			this.renderJson(result, response);
		}
	}
	
	@RequestMapping(value = "hasresourcelist")
	@ResponseBody
	public void hasresourcelist(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		String order = request.getParameter("order");
		if(null!=offset&&null!=limit){
			Page<SysmanResource> page=new Page<SysmanResource>();
			page.setOffset(Integer.parseInt(offset));
			page.setLimit(Integer.parseInt(limit));
			page.setOrder(order);
			List<SysmanResource> userlist=this.getService().getHasResourceList();
			page.setRows(userlist);
			this.renderJson(page, response);
		}else{
			Result result=new Result();
			result=Result.fail("参数错误");
			this.renderJson(result, response);
		}
	}
	/**
	 * 转换为tree类型
	 * @param sysResourceList  资源list集合
	 * @return
	 */
	private List<Map<String, Object>> resourceToTree(List<SysmanResource> sysResourceList) {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		for(SysmanResource r : sysResourceList){
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("resourceType", r.getResourceType());
			item.put("name", r.getName());
			item.put("description", r.getDescription());
			item.put("orderNo", r.getOrderNo());
			item.put("parent.pid", r.getParent() == null ? "" : r.getParent().getPid());
			item.put("parent.name", r.getParent() == null ? "" : r.getParent().getName());
			if (r.getSubResource() != null && r.getSubResource().size() != 0) {
				item.put("children", this.resourceToTree(r.getSubResource()));
				item.put("leaf", false);
			}else{
				item.put("leaf", true);
			}
			item.put("href", r.getHref());
			item.put("pid", r.getPid());
			item.put("deleteFlag", r.getDeleteFlag());
			item.put("createTime", r.getCreateTime());
			
			data.add(item);
		}
		return data;
	}

}
